package com.sichen.auth.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.sichen.auth.constant.GiteeConstant;
import com.sichen.auth.feign.MemberFeignService;
import com.sichen.common.vo.MemberRespVo;
import com.sichen.auth.vo.SocialUserVo;
import com.sichen.common.utils.HttpUtils;
import com.sichen.common.utils.R;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * 处理OAuth登录的Controller
 * <p>Created with IntelliJ IDEA.
 * <p>Author: 思尘
 * <p>Created time: 2022-09-21 20:46
 * <p>Version: V1.0
 */
@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;


    @GetMapping("/oauth/success")
    public String gitee(@RequestParam("code") String code, HttpSession session, HttpServletResponse servletResponse) throws Exception {
        // 1.根据code换取AccessToken
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("grant_type", GiteeConstant.GITEE_GRANT_TYPE);
        map.put("code", code);
        map.put("client_id", GiteeConstant.GITEE_CLIENT_ID);
        map.put("redirect_uri", GiteeConstant.GITEE_REDIRECT_URI);
        map.put("client_secret", GiteeConstant.GITEE_CLIENT_SECRET);
        // https://gitee.com/oauth/token
        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token",
                "post", null, null, map);
        // 2.处理
        if (response.getStatusLine().getStatusCode() == 200) {
            String json = EntityUtils.toString(response.getEntity());
            SocialUserVo socialUserVo = JSON.parseObject(json, SocialUserVo.class);
            // 1.当前用户如果是第一次进网站 , 自动注册进来
            // (为当前社交用户生成一个会员信息账号 , 以后这个社交用户就对应指定的会员)
            // 调用远程服务 , 判断是登录还是注册
            R oauthLogin = memberFeignService.oauthLogin(socialUserVo);
            if (oauthLogin.getCode() == 0) {
                // 2. 登录成功就跳回首页
                MemberRespVo data = oauthLogin.getData("data", new TypeReference<MemberRespVo>() {});
                // 封装数据
                // 1.第一次使用session , 命令浏览器保存卡号 , JSESSIONID 这个cookie
                // 以后浏览器访问那个网站就会带上这个网站的cookie
                // 发卡的时候(指定域名为父域名) , 即使是子域系统发的卡 , 也能让父域直接使用
                // TODO 1,默认发的令牌 作用域: 当前域 , (解决子域session共享问题)
                // TODO 2.使用JSON的序列化对象到redis中
                session.setAttribute(GiteeConstant.GITEE_OAUTH_SESSION_KEY,data);
               // servletResponse.addCookie(new Cookie("JSESSIONID",data));
                return "redirect:http://sichen.com";
            } else {
                return "redirect:http://auth.sichen.com/login.html";
            }
        } else {
            return "redirect:http://auth.sichen.com/login.html";
        }
    }
}
